<?php

class Maintenance {

    public $newUrlPage;
    public $deleteUrlPage;
    public $editUrlPage;
    public $saveNewUrlPage;
    public $updateUrlPage;
    public $listJsonUrl;
    public $windowTittle;
    public $tittle;
    public $aditionalScript;
    public $globalCallBackJs;
    public $path = '/';
    public $editUrlParams = array();
    public $deleteUrlParams = array();
    public $staticsUrl;
    public $validations = array();
    public $table = "";
    public $formName = "form1";
    public $urlBack = NULL;
    public $panels = FALSE;

    function loadDynValidations($db) {
        $where = sprintf("TABLE_NAME='%s' AND STATUS=1", $this->table);
        $rs = $db->query_select("cllg_tablefield", $where);
        foreach ($rs as $r) {
            //datatype validations
            $field = "," . $r['NAME'] . ",";
            $rule = "";
            switch ($r['TYPE']) {
                case '1':
                    $rule = "is_alpha" . $field . $r['CAPTION'] . " debe contener letras o numeros!";  //string
                    break;
                case '2':
                    $rule = "digits_only" . $field . $r['CAPTION'] . " es de tipo entero!";  //integer
                    break;
                case '3':
                    $rule = "decimalNumber" . $field . $r['CAPTION'] . " es de tipo decimal!";
                    ;  //string
                    break;
                case '5':
                    $rule = "valid_email" . $field . $r['CAPTION'] . " es invalido!";
                    ;  //string
                    break;
            }
            if ($rule != "")
                $this->validations[] = $rule;

            //validation required
            if ($r['REQUIRED'] == '1') {
                $rule = "required" . $field . $r['CAPTION'] . " es requerido!";
                $this->validations[] = $rule;
            }
        }
    }

    function style() {
        echo '<link href="' . $this->path . 'bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />';
        echo '<link href="' . $this->path . 'css/tableorderer.css" rel="stylesheet" type="text/css" />';
        #echo '<link href="' . $this->path . 'css/maintenance.css" rel="stylesheet" type="text/css" />';
        #echo '<link href="' . $this->path . 'css/ContentStyle.css" rel="stylesheet" type="text/css" />';
        echo '<link href="' . $this->path . 'js/protocalendar/datepicker.css" rel="stylesheet" type="text/css" />';
        echo '<link href="' . $this->path . 'js/dialog/dialog.2.0.css" rel="stylesheet" type="text/css" />';
        echo '<link href="' . $this->path . 'js/ui/jquery.ui.min.css" rel="stylesheet" type="text/css" />';
    }

    function script() {
        echo '<script src="' . $this->path . 'js/prototype.js" type="text/javascript"></script>';
        echo '<script src="' . $this->path . 'js/jquery.js" type="text/javascript"></script>';
        echo '<script src="' . $this->path . 'js/jquery.ui.min.js" type="text/javascript"></script>';
        echo '<script src="' . $this->path . 'bootstrap/js/bootstrap.js" type="text/javascript"></script>';
        echo '<script>jQuery.noConflict();</script>';
        echo '<script src="' . $this->path . 'js/protocalendar/prototype-date-extensions.js" type="text/javascript"></script>';
        echo '<script src="' . $this->path . 'js/protocalendar/datepicker.js"  type="text/javascript"></script>';
        echo '<script src="' . $this->path . 'js/TableOrderer.js" type="text/javascript"></script>';
        echo '<script src="' . $this->path . 'js/jsvalidate.js" type="text/javascript"></script>';
        echo '<script src="' . $this->path . 'js/dialog/dialog.2.0.js" type="text/javascript"></script>';
        echo '<script src="' . $this->path . 'js/include.js" type="text/javascript"></script>';
    }

    function panelScript() {
        echo '<script src="../../SpryAssets/SpryTabbedPanels.js" type="text/javascript"></script>';
        echo '<link href="../../SpryAssets/SpryTabbedPanels.css" rel="stylesheet" type="text/css" />';
    }

    function head() {
        echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
              <html xmlns="http://www.w3.org/1999/xhtml">';
        echo '<head>';
        echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
        echo '<title>' . $this->windowTitle . '</title>';
        if ($this->panels) {
            $this->panelScript();
        }
        $this->style();
        $this->script();
        $this->jsConfig();
        if ($this->aditionalScript != NULL) {
            include($this->aditionalScript);
        }
        echo '</head>';
    }

    function getDeleteUrl() {
        $rowTbl = 'runningTable.selected';
        $params = '';
        $index = 0;
        foreach ($this->deleteUrlParams as $param => $value) {
            $params.= ($index == 0) ? "" : "+'&'";
            $params.="'" . $param . "='+" . $rowTbl . "." . $value;
            $index++;
        }
        echo $params;
    }

    function jsConfig() {

        $rowTbl = 'runningTable.selected';
        $params = '';
        $index = 0;
        $extContent = "";
        foreach ($this->editUrlParams as $param => $value) {
            $params.= ($index == 0) ? "" : "+'&'";
            $params.="'" . $param . "='+" . $rowTbl . "." . $value;
            $index++;
        }
        if ($this->globalCallBackJs != NULL) {
            include($this->globalCallBackJs);
        }

        echo "<script type='text/javascript'>
		function loadRowSelect(){
		  if(runningTable!=null && runningTable.selected){
			  loadPage('" . $this->editUrlPage . "'," . $params . ");
                          jQuery('#cmAddcontainer,#cmBack').toggle();
		  }
		}
		globalCallBack=function(){
			" . $this->scriptValidations()
        . "
             " . $extContent .
        "
		}
		maintenanceCfg={jsonUrl:'" . $this->listJsonUrl . "',callBack:loadRowSelect,staticsUrl:'" . $this->staticsUrl . "'};
		addOnLoad(loadTable);	
		</script>";
    }

    function build() {
        $this->head();
        $this->foot();
    }

    function foot() {
        $urlClick = "'" . $this->newUrlPage . "'";
        echo '<body>';
        echo '<div id="rsvErrors"></div>';
        echo '<div id="controls">';
        echo '<div id="title"><ul class="nav nav-tabs"><li class="active" style="font-weight:bolder"><a href="">' . $this->windowTitle . '</a></li></ul></div><br/>';
        echo '<script type="text/javascript">
                function loadPageNew(){
                    loadPage(' . $urlClick . ');
                    jQuery("#cmAddcontainer").hide();
                    jQuery("#cmBack").hide();
                };</script>';
        echo '<div id="cmAddcontainer" style="margin-left:75%"><input name="cmdAdd" class="btn btn-info" type="button" value="Agregar nuevo" onclick="loadPageNew()" /></div><br/>';
        if ($this->urlBack != NULL) {
            $urlClick = "'" . $this->urlBack . "'";
            echo '<input name="cmBack" id="cmBack" style="margin-left:45%" class="btn btn-info" type="button" onclick="window.history.back()"' .
            $urlClick . ')" value="Regresar" />';
        }
        echo '	</div>';
        echo '	<div id="tabla"></div>';
        echo '	<div id="formulario"></div>';
        echo '	</body>';
        echo '  <div id="error"></div>';
        echo '</html>';
    }

    function statics($num) {
        echo '<table width="100%" class="" border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<th scope="col">Información</th>
		  </tr>
		  <tr>
			<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
			  
			  <tr>
				<td width="">Total de Registros:</td>
				<td width="">' . $num . '</td>
			  </tr>
			  <tr>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
			  </tr>
			</table></td>
		  </tr>
		</table>';
    }

    function scriptValidations() {
        $rules = "";
        $index = 0;
        foreach ($this->validations as $v) {
            $rules.= ($index == 0) ? "" : ",";
            $rules.="'" . $v . "'";
            $index++;
        }
        return '
	  var form=document.' . $this->formName . ';
	  if(form){
		 rsv= new RSV({
					formID: form,
					errorFieldClass: "errorField",
					rules: [' . $rules . '],
					handlerParam: form,
					displayType : "display-html",
					errorHTMLItemBullet : "&#8212; "
				});
	 }';
    }

}

?>